#delimit;
set more off;

* download Pew data and convert Dec02c.sav to .dta format prior to running code;
use Dec02c.dta, clear;

recode q5a (1=4)(2=3)(3=2)(4=1)(5/9=.), gen(opfed);
recode q5b (1=4)(2=3)(3=2)(4=1)(5/9=.), gen(opst);
recode q5c (1=4)(2=3)(3=2)(4=1)(5/9=.), gen(oploc);

recode q1 	(1=1 "Approve")
		(2=0 "Disapprove")
		(9=.), gen(approve);

recode sex (1=1)(2=0), gen(r_male);
recode age (99=.), gen(r_age);
recode educ (9=.), gen(r_educ);
recode d10 (9=.), gen(r_income);
recode hisp (1=1)(2=0)(9=.), gen(r_hisp);
recode race (1=1)(2/4=0)(9=.), gen(r_white);
   replace r_white=0 if r_hisp==1;
recode race (2=1)(1=0)(3/4=0)(9=.), gen(r_black);
   replace r_black=0 if r_hisp==1;
recode race (3=1)(1=0)(2 4=0)(9=.), gen(r_asian);
   replace r_asian=0 if r_hisp==1;
   
rename d6 marital;

recode marital 	(1=1 "Married")
				(2 3=2 "Divorced")
				(3=3 "Widowed")
				(4=4 "Single")(9=.), gen(r_marital);
tab marital, gen(r_marital);

recode d9
           (1=1 "Yes, registered")
           (2=0 "No, not registered")
           (9=. ), gen(r_regist); 

recode d7 (1=1)(2=0)(9=.), gen(r_parent);		   
		   
recode d12 (1=1)(2/5=0)(9=.), gen(gop);
recode d12 (1=0)(2=1)(3/5=0)(9=.), gen(dem);
recode d12 (1/2=0)(3/5=1)(9=.), gen(ind);

recode d12 (1=1 "Republican")(2=5 "Democrat") (3/5=3 "Independent")(9=.), gen(partyid);
replace partyid=2 if partyid==3 & d12a==1;
replace partyid=3 if partyid==3 & d12a==9;
replace partyid=4 if partyid==3 & d12a==2;

recode d14 (9=.), gen(ideo_all);
tab ideo_all, gen(ideo);

recode d9 (1=1)(2=0)(.=0)(9=.), gen(r_bornagn);

recode d15 (1=1)(2/3=0)(9=.), gen(r_ownhome);

recode d16 (9=.),gen(employ);
tab employ, gen(employ);

recode d18 (1/3=1)(4=0)(9=.), gen(r_union);


tostring fips, replace;
gen new=substr(fips, -3, 3);
destring fips, replace;
destring new, replace;
replace fips=(fips-new)/1000;
drop new;
gen year=2002;


keep fips r_* op* approve gop dem ind partyid ideo* employ* year weight;

codebook r_* op* approve gop dem ind partyid ideo* employ*;
